<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputimage} function plugin
 *
 * File:       function.pp_inputimage.php<br>
 * Type:       function<br>
 * Name:       pp_inputimage<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_inputimage values=$ids output=$names}
 * {pp_inputimage values=$ids name='box' separator='<br>' output=$names}
 * {pp_inputimage values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_inputimage}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputimage($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

	$name = NULL;
	$value = NULL;
	$text = NULL;
	$url = NULL;
	$extra = NULL;
	$lw = $lh = $width = $height = '';
	$require = NULL;
	$requirement = NULL;
	$id = NULL;
	$dsup = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'name':
				$$_key = (string)$_val;
				break;
			
			case 'value':
				$$_key = (string)$_val;
				break;
			
			case 'text':
				$$_key = (string)$_val;
				break;
			
			case 'url':
				$$_key = (string)$_val;
				break;
			
			case 'require':
				$$_key = (string)$_val;
				break;
				
			case 'requirement':
				$$_key = (string)$_val;
				break;
			
			case 'class':
				break;
			
			case 'lw':
			case 'lh':
			case 'width':
			case 'height':
				$$_key = (string)$_val;
			
			case 'assign':
				break;
			
			case 'id':
				$$_key = (string)$_val;
				break;
				
			case 'dsup':
				$$_key = (string)$_val;
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_inputimage: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_inputimage_output($name, $value, $url, $require, $requirement, $extra, $lw, $lh, $width, $height, $id, $dsup);

	return $_html_result;
}

function smarty_function_pp_inputimage_output($name, $value, $url, $require, $requirement, $extra, $lw, $lh, $width, $height, $id, $dsup) {
	$_output = '';
	
	//
	if( substr($url, 0, 3) == '===' ) {
		$url = substr($url, 3);
		if( strstr($url, ',') === false ) {
			$urls = array($url);
		} else {
			$urls = explode(',', $url);
		}
		$url = '';
		foreach($urls as $k => $v) {
			$url .= constant($v);
		}
	}
	
	//if( isset($_REQUEST[$name]) ) {
	//	$value = (string)$_REQUEST[$name];
	//}
	if( strstr($name, '[]') === false ) {
		if( isset($_REQUEST[$name]) ) {
			$value = (string)$_REQUEST[$name];
		}
	} else {
		$nameb = str_replace('[]', '', $name);
		if( !isset($_REQUEST[$nameb . '_index']) ) {
			$_REQUEST[$nameb . '_index'] = 0;
		}
		if( isset($_REQUEST[$nameb][$_REQUEST[$nameb . '_index']]) ) {
			$value = (string)$_REQUEST[$nameb][$_REQUEST[$nameb . '_index']];
		}
		$_REQUEST[$nameb . '_index']++;
	}
	
	//
	if( $id != '' ) {
		$id_h = ' id="' . $id . '"';
		$id_f = ' id="' . $id . '_file"';
		$id_img = ' id="' . $id . '_img"';
	} else {
		$id_h = $id_f = $id_img = '';	
	}
	
	//
	$_output .= '<input type="hidden" name="' . smarty_function_escape_special_chars($name) . '" value="' . smarty_function_escape_special_chars($value) . '"'.$id_h.' />';
	if( !isset($dsup) ) {
		$_output .= '<input type="file" name="' . smarty_function_escape_special_chars($name) . 'upload" value=""';
		$_output .= $id_f;
		$_output .= $extra . '>';
	}
	if( $require == true ) {
		if( $_REQUEST['_SUBMITTED_'] == 1 ) {
			if( isset($_REQUEST[$name]) && $_REQUEST[$name] == "" ) {
				if( $requirement ) {
					$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
				} else {
					$_output .= '<span class="inputRequirement">*</span>';
				}
			} elseif( isset($_REQUEST[$name]) && $_REQUEST[$name] != "" ) {
				//
			}
		} else {
			if( $requirement ) {
				$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
			} else {
				$_output .= '<span class="inputRequirement">*</span>';
			}
		}
	}
	if( $value != '' ) {
		$src = $value;
		$_output .= '<br>';
		//$_output .= '<img src="' . $url . $value . '">';
		if( $lw == '' && $lh == '' ) {
			if( strpos($url, 'thumb') !== false ) {
				$lw = 200;
				$lh = 200;
			} elseif( strpos($url, 'resize') !== false ) {
				$lw = 500;
				$lh = 500;
			}
		}
		
		if( $height > 0 && $width > 0 && $lw > 0 && $lh > 0 ) {
			$ratio = $width / $height;
			$ratio_set = $lw / $lh;
			if( $ratio >= $ratio_set ) {
				$lh = ceil($height * ( $lw / $width ));
			} else {
				$lw = ceil($width * ( $lh / $height ));
			}
		}
		
		if( $lw > 0 && $lh > 0 ) {
			$_output .= $prefix . '<img src="'.$url.$src.'" alt="'.$alt.'" width="'.$lw.'" height="'.$lh.'"'.$id_img.$extra . '> '. $suffix;
		} elseif( $width > 0 && $height > 0 ) {
			$_output .= $prefix . '<img src="'.$url.$src.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$id_img.$extra . '> '. $suffix;
		} else {
			$_output .= $prefix . '<img src="'.$url.$src.'" alt="'.$alt.'" '.$extra . '> '. $suffix;
		}
	}
	
	return $_output;
}
//
?>